package com.shuogesha.common.util;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

public class JsonResult<T> {
	private String code;
	private String message;
	private T data;

	public JsonResult(String code, String message, T data) {
		super();
		this.code = code;
		this.message = message;
		this.data = data;
	}

	public JsonResult() {
		this.setCode(ResultCode.SUCCESS.val());
		this.setMessage(ResultCode.SUCCESS.msg());
	}

	public JsonResult(ResultCode r) {
		this.setCode(r.val());
		this.setMessage(r.msg());
	}

//	public JsonResult(ResultCode code, String message) {
//		this.setCode(code);
//		this.setMessage(message);
//	}

	public JsonResult(ResultCode resultCode, String message, T data) {
		this.setCode(resultCode.val());
		this.setMessage(message);
		this.setData(data);
	}

	public JsonResult(ResultCode resultCode, T data) {
		this.setCode(resultCode.val());
		this.setData(data);
	}

	public String toString() {
		JSONObject json = new JSONObject();
		try {
			json.put("code", code);
			json.put("message", message);
			json.put("data", data);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return json.toString();
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
}
